package com.study.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.study.pojo.user;
import com.study.dao.userdao;
import com.study.service.userservice;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//@WebServlet("*.user")
public class userServlet extends HttpServlet {
    public userServlet() {
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("GBK");
        String url=request.getServletPath();
       if(url.equals("/register.user")){
            String username = request.getParameter("username");
            user ui=new user();
            ui.setUsername(username);
            ResultSet rs=null;
            try {
                rs = userdao.selectbyusername(ui);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(rs.next()) {
                    request.setAttribute("msg","用户已存在！");
                    request.getRequestDispatcher("/registerUI.jsp").forward(request, response);

                }else {
                    String password = request.getParameter("password");
                    ui.setUsername(username);
                    ui.setPassword(password);
                    int i = userservice.register(ui);
                    if (i > 0){
                        request.setAttribute("msg", "注册成功！");
                    request.getRequestDispatcher("/registerUI.jsp").forward(request, response);}
                    else{
                        request.setAttribute("msg", "注册失败！");
                    request.getRequestDispatcher("/registerUI.jsp").forward(request, response);}
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else if(url.equals("/login.user")){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            user ui = new user();
            ui.setUsername(username);
            int i=0;
            try{
                ui.setPassword(password);
                i=userservice.login(ui);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try{
                if(i>0){
                    // 获取 pageSize 和 pageNumber
                    String pageSize = request.getParameter("pageSize");
                    String pageNumber = request.getParameter("pageNumber");
                    // 首页的分页数据初始化
                    if("".equals(pageSize)||"".equals(pageNumber)
                            ||null == pageSize || null == pageNumber){
                        pageNumber = "1";
                        pageSize = "5";
                    }
                    Integer maxPage = userservice.maxPage(Integer.parseInt(pageSize));
                    // 判断是否为最小页数
                    if(Integer.parseInt(pageNumber)<=0){
                        pageNumber = "1";
                    }
                    // 判断是否为最大页数
                    if(Integer.parseInt(pageNumber)>=maxPage){
                        pageNumber = maxPage+"";
                    }

                    // 如何获取总页数

                    // 获取 起始值 和结束值
                    // ((当前页-1)*每页行数)+1 = 起始行数
//        Integer start = ((Integer.parseInt(pageNumber)-1)*Integer.parseInt(pageSize)+1);
//        Integer end = Integer.parseInt(pageNumber)*Integer.parseInt(pageSize);
//        List<User> userList = userService.pagination(start,end);

                    List<user> userList = userservice.page(Integer.parseInt(pageSize),
                            (Integer.parseInt(pageNumber)));
                    // 放入 request 域中
                    request.setAttribute("userList",userList);
                    request.setAttribute("pageNumber",pageNumber);
                    request.setAttribute("maxPage",maxPage);
                    // 跳转至 list.jsp 页面
                    request.getRequestDispatcher("/userlist1.jsp").forward(request, response);
                }else {
                    request.setAttribute("msg","登入失败！请重新登入！");
                    request.getRequestDispatcher("/login1.jsp").forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if(url.equals("/update.user")){
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String id=request.getParameter("id");
            user ui = new user();int i=0;
            ui.setUsername(username);
            ui.setPassword(password);
            ui.setId(Integer.parseInt(id));
            try{
                i=userservice.update(ui);
            } catch (Exception e) {
                e.printStackTrace();
            }if(i>0) {
                request.setAttribute("msg","修改成功！");
                request.getRequestDispatcher("/updateUI.jsp").forward(request, response);
            }else {
                request.setAttribute("msg","修改失败！");
                request.getRequestDispatcher("/updateUI.jsp").forward(request, response);
            }
        } else if(url.equals("/delete.user")){
          // String username = request.getParameter("username");
           // String password = request.getParameter("password");
           String id=request.getParameter("id");
            user ui = new user();int i=0;
            ui.setId(Integer.parseInt(id));
           // ui.setUsername(username);
           // ui.setPassword(password);
            try{
                i=userservice.deletebyid(ui);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(i>0){
                request.setAttribute("msg","删除成功！");
               request.getRequestDispatcher("/list.user").forward(request, response);
            }else{
                request.setAttribute("msg","删除失败！");
              request.getRequestDispatcher("/list.user").forward(request, response);
            }
        }else if(url.equals("/list.user")){
           // 获取 pageSize 和 pageNumber
           String pageSize = request.getParameter("pageSize");
           String pageNumber = request.getParameter("pageNumber");
           // 首页的分页数据初始化
           if("".equals(pageSize)||"".equals(pageNumber)
                   ||null == pageSize || null == pageNumber){
               pageNumber = "1";
               pageSize = "5";
           }
           Integer maxPage = userservice.maxPage(Integer.parseInt(pageSize));
           // 判断是否为最小页数
           if(Integer.parseInt(pageNumber)<=0){
               pageNumber = "1";
           }
           // 判断是否为最大页数
           if(Integer.parseInt(pageNumber)>=maxPage){
               pageNumber = maxPage+"";
           }

           // 如何获取总页数

           // 获取 起始值 和结束值
           // ((当前页-1)*每页行数)+1 = 起始行数
//        Integer start = ((Integer.parseInt(pageNumber)-1)*Integer.parseInt(pageSize)+1);
//        Integer end = Integer.parseInt(pageNumber)*Integer.parseInt(pageSize);
//        List<User> userList = userService.pagination(start,end);

           List<user> userList = userservice.page(Integer.parseInt(pageSize),
                   (Integer.parseInt(pageNumber)));
           // 放入 request 域中
           request.setAttribute("userList",userList);
           request.setAttribute("pageNumber",pageNumber);
           request.setAttribute("maxPage",maxPage);
           // 跳转至 list.jsp 页面
            request.getRequestDispatcher("/userlist1.jsp").forward(request, response);
        }

    }
}
